#ifndef HELMETEXTERNALINTERRUPTS_H
#define HELMETEXTERNALINTERRUPTS_H

#include "HelmetGlobals.h"

/**************************************************************************************
	Definitions
**************************************************************************************/


/**************************************************************************************
	Global Variables
**************************************************************************************/


/**************************************************************************************
	Function Prototypes
**************************************************************************************/


/**************************************************************************************
	Functions
**************************************************************************************/
void ConfigExternalInterrupts() {
	
	// Power/Sleep Button
    mPORTESetPinsDigitalIn(BIT_8);
    ConfigINT1( EXT_INT_PRI_7 | FALLING_EDGE_INT | EXT_INT_ENABLE );
    
    // Radio Button Pad
    //mPORTESetPinsDigitalIn(BIT_9)
    ConfigINT2( EXT_INT_PRI_6 | RISING_EDGE_INT | EXT_INT_ENABLE );
		
}

/**************************************************************************************
	Interrupt handling routines
**************************************************************************************/
// Note: Priority 1 (lowest) to 7 (highest)

/**************************************
		Power/Sleep Button
			Priority 7
**************************************/
void __ISR(_EXTERNAL_1_VECTOR, ipl7) INT1Handler(void) 
{ 
	mINT1ClearIntFlag();
	
	// check if it's a human press, not a jitter
	int i=160000;
	while( i>0 ) i--;
	
	if( mPORTEReadBits( BIT_8 ) == 0 ) {
		// good, so do stuff ...
		if( Sleeping == 1 ) {
			Sleeping = 0;
			DoInMain( ExitSleepMode );
		} else if( EnterSleepMode == 1 ) {
			// do nothing
		} else {
			DoInMain( EnterSleepMode );
		}
	} else {
		// jitter, so do nothing ...
	}
	
	mINT1ClearIntFlag();
}

/**************************************
		Power/Sleep Button
			Priority 6
**************************************/
void __ISR(_EXTERNAL_2_VECTOR, ipl6) INT2Handler(void) 
{ 
	mINT2ClearIntFlag();
	if( Sleeping == 1 ) {
		WriteAtom("Sleep Button was pressed. Going back to sleep.\r\n");
		PowerSaveSleep();
	} else {
		DoInMain( CheckRemoteButtonPad );
	}	
} 

/**************************************
		Radio Button Pad
			Priority 7
**************************************/

#endif